package com.lizhe.config.config;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.util.List;
import java.util.Map;

/**
 * 类描述: 读取 employee.properties 配置
 *        使用 SPEL 表达式处理获取的结果
 *
 * @author Administrator
 * @create 2021-10-26 20:44
 */
@Data
@Configuration
@PropertySource(name = "employeeProperties",
        value = "classpath:employee.properties",
        encoding = "utf-8")
public class Employee {

    //使用SpEL读取employee.properties配置文件
    @Value("#{'${employee.names}'.split(',')}")
    private List<String> employeeNames;

    // 获取转换成 map
    @Value("#{${employee.age}}")
    private Map<String, Integer> employeeIdMap;

    @Value("#{${employee.age}.two}")
    // @Value ("#{${employee.age}['two']}")  //这样写也可以
    // @Value ("#{${employee.age}['five'] ?: 31}") // 甚至可以写表达式，如果不确定该值是否存在
    private String employeeAgeTwo;

    @Value("#{'${employee.names}'.split(',')[0]}")
    private String firstEmployName;
}